﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericGA.reader.regex
{
    /// <summary>
    /// A regular expression node for disjuncting (ORing) child regular expression nodes.
    /// </summary>
    /// <typeparam name="A">The type of the alphabet being matched.</typeparam>
    public class Disjunc<A> : Regex<A>
    {

        /// <summary>
        /// The operand regular expressions of the concatenation.
        /// </summary>
        private readonly Regex<A>[] operands;

        /// <summary>
        /// Constructor to disjunct an array of regular expressions.
        /// </summary>
        /// <param name="operands">Regular expressions to disjunct together.</param>
        public Disjunc(params Regex<A>[] operands)
        {
            this.operands = operands;
        }

        /// <summary>
        /// Generate a list which is matched by the regular expression based on a reader object.  In this case, the disjunction of operand regular expressions.
        /// </summary>
        /// <param name="reader">Reader object to determine which list to generate.</param>
        /// <returns>The disjunction of regular expressions.</returns>
        public override List<A> Generate(Reader reader)
        {
            Regex<A> operand = reader.ReadElem<Regex<A>>(operands);
            return operand.Generate(reader);
        }

        /// <summary>
        /// Enumerate every list which matches this regular expression.
        /// </summary>
        /// <returns>An iteration of the lists matched by the regular expression.</returns>
        public override IEnumerable<List<A>> Enumerate()
        {
            foreach (Regex<A> operand in operands)
            {
                foreach (List<A> result in operand.Enumerate())
                {
                    yield return result;
                }
            }
        }

    }
}
